package org.cloudie.docs.editor.client.content.dialogs;

import org.cloudie.docs.editor.client.commands.Command;
import org.cloudie.docs.editor.client.events.CommandEvent;
import org.cloudie.docs.editor.client.widgets.Dialog;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * A generic dialog window displaying a message and an action button.
 */
public class ActionDialog extends Dialog {

  protected static ActionDialog instance;
  
  public static ActionDialog get() {
    if (instance == null) {
      instance = new ActionDialog();
    }
    return instance;
  }
  
  protected HTML message;
  protected Command actionCommand;
  protected Button actionButton, cancelButton;
  
  /**
   * Constructs a Loading dialog window.
   */
  protected ActionDialog() {
    super("", true);
    ClickHandler cancelHandler = new ClickHandler(){
      public void onClick(ClickEvent event) {
        hide();
      }
    };
    closeButton.addClickHandler(cancelHandler);
    actionButton = new Button("Action", new ClickHandler(){
      public void onClick(ClickEvent event) {
        hide();
        if (actionCommand != null) {
          CommandEvent.fire(actionCommand);
        }
      }
    });
    cancelButton = new Button("Cancel", cancelHandler);
    HorizontalPanel buttonPanel = new HorizontalPanel();
    buttonPanel.setSpacing(10);
    buttonPanel.add(actionButton);
    buttonPanel.add(cancelButton);
    message = new HTML();
    VerticalPanel content = new VerticalPanel();
    content.setWidth("500px");
    content.add(message);
    content.add(buttonPanel);
    setContentWidget(content);
  }
  
  public void update(String title, String actionName, String message,
		boolean allowCancel, Command actionCommand) {
	this.titleLabel.setText(title);
	this.actionCommand = actionCommand;
	this.actionButton.setText(actionName);
	this.message.setHTML(message);
	this.cancelButton.setVisible(allowCancel);
  }

}
